<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item prop="label">
          <el-input v-model="filters.label" placeholder="标签"></el-input>
        </el-form-item>
        <el-form-item prop="value">
          <el-input v-model="filters.value" placeholder="值"></el-input>
        </el-form-item>
        <el-form-item prop="type">
          <el-input v-model="filters.type" placeholder="类型"></el-input>
        </el-form-item>
        <el-form-item>
          <kt-button icon="fa fa-search"
                     :label="$t('action.search')"
                     perms="sys:dict:view"
                     type="primary"
                     @click="findPage"/>
        </el-form-item>
        <el-form-item>
          <kt-button icon="fa fa-refresh"
                     :label="$t('action.reset')"
                     perms="sys:dict:view"
                     type="primary"
                     @click="reset"/>
        </el-form-item>
        <el-form-item>
          <kt-button icon="fa fa-plus"
                     :label="$t('action.add')"
                     perms="sys:dict:add"
                     type="primary"
                     @click="showAdd"/>
        </el-form-item>
      </el-form>
    </div>

    <!--表格内容栏-->
    <kt-table permsEdit="sys:dict:edit" permsDelete="sys:dict:delete"
              :data="pageResult" :columns="columns"
              @findPage="findPage" @handleEdit="showEdit" @handleDelete="handleDelete">
    </kt-table>

    <!--新增编辑界面-->
    <el-dialog :title="operation?'新增':'编辑'" width="25%" :visible.sync="editDialogVisible" :close-on-click-modal="false">
      <el-form :model="dataForm" label-width="80px" :rules="dataFormRules" ref="dataForm" :size="size">
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="标签" prop="label">
          <el-input v-model="dataForm.label" :disabled="!operation"></el-input>
        </el-form-item>
        <el-form-item label="值" prop="value">
          <el-input v-model="dataForm.value"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input v-model="dataForm.type" :disabled="!operation"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="dataForm.sort"></el-input>
        </el-form-item>
        <el-form-item label="描述 " prop="description">
          <el-input v-model="dataForm.description" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="dataForm.remarks" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button :size="size" type="primary" @click.native="submitForm" :loading="editLoading">
          {{$t('action.submit')}}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import KtTable from '../core/KtTable'
  import KtButton from '../core/KtButton'
  import {format} from "../../utils/datetime";

  export default {
    name: "Dict",
    components: {
      KtButton,
      KtTable
    },
    data() {
      return {
        size: 'small',
        filters: {
          label: '',
          type: '',
          value: ''
        },
        columns: [
          {prop: "id", label: "ID", minWidth: 50},
          {prop: "label", label: "标签", showOverFlow: true, minWidth: 150},
          {prop: "value", label: "值", showOverFlow: true, minWidth: 120},
          {prop: "type", label: "类型", showOverFlow: true, minWidth: 120},
          {prop: "sort", label: "排序", minWidth: 40},
          {prop: "description", label: "描述", showOverFlow: true, minWidth: 120},
          {prop: "remarks", label: "备注", showOverFlow: true, minWidth: 120},
          {prop: "lastUpdator", label: "最近操作者", minWidth: 100},
          {prop: "lastUpdateTime", label: "最近操作时间", minWidth: 135}
        ],
        pageRequest: {},
        pageResult: {},
        operation: false, // true:新增, false:编辑
        editDialogVisible: false, // 新增编辑界面是否显示
        editLoading: false,
        dataFormRules: {
          label: [
            {required: true, message: '请输入名称', trigger: 'blur'},
          ],
          value: [
            {required: true, message: '请填写对应的值', trigger: 'blur'}
          ],
          type: [
            {required: true, message: '请定义类型', trigger: 'blur'}
          ],
          description: [
            {required: true, message: '请添加描述', trigger: 'blur'}
          ]
        },
        // 新增编辑界面数据
        dataForm: {
          id: 0,
          label: '',
          value: '',
          type: '',
          sort: 0,
          description: '',
          remarks: ''
        }
      }
    },
    methods: {
      //分页查询
      findPage(data) {
        if (data !== null && typeof data.pageRequest !== 'undefined') {
          this.pageRequest = data.pageRequest;
        }
        this.pageRequest.params = {
          label: this.filters.label,
          type: this.filters.type,
          value: this.filters.value,
        };
        this.$api.dict.findPage(this.pageRequest).then((res) => {
          this.pageResult = res.data;
        }).then(data !== null ? data.callback : '')
      },
      //批量删除
      handleDelete(data) {
        this.$api.dict.batchDelete(data.params).then(data !== null ? data.callback : '')
      },
      //显示新增界面
      showAdd() {
        this.editDialogVisible = true;
        this.operation = true;
      },
      //显示编辑界面
      showEdit(params) {
        this.editDialogVisible = true;
        this.operation = false;
        this.dataForm = Object.assign({}, params.row);
      },
      // 编辑
      submitForm: function () {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;
              this.$api.dict.save(this.dataForm).then((res) => {
                this.editLoading = false;
                if (res.code === 200) {
                  this.$message({message: '保存成功', type: 'success'})
                } else {
                  this.$message({message: res.msg, type: 'error'})
                }
                this.$refs['dataForm'].resetFields();
                this.editDialogVisible = false;
                this.findPage();
              })
            })
          }
        })
      },
      reset() {
        this.$refs['filters'].resetFields();
        this.findPage();
      },
    }
  }
</script>

<style scoped>

</style>
